/**
 * 
 */
package hu.cubussapiens.client.core.query;

import java.io.File;
import java.io.IOException;

import hu.cubussapiens.client.core.Activator;
import hu.cubussapiens.client.core.login.AppManager;

import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.FileRequestEntity;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;

/**
 * @author balazs.grill
 *
 */
public class DataManager {

	private final AppManager appman;
	
	public DataManager(AppManager appman) {
		this.appman = appman;
	}
	
	public int upload(String id, RequestEntity rentity) throws HttpException, IOException{
		PutMethod method = new PutMethod(appman.getApproot()+"data?literal="+id+"&contenttype="+rentity.getContentType());
		method.setRequestEntity(rentity);
		return appman.execute(method);
	}
	
	public void upload(String id, File file, String contentType) throws CoreException{
		try {
			int status = upload(id, new FileRequestEntity(file, contentType));
			if (status != 200) throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Operation returned status "+status));
		} catch (Exception e) {
			throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));
		}
	}
	
}
